#!/bin/bash
#############################################
# 删除原有内容
 


# 搜索文件中的 # >>>> start auto ftun
start_marker="# >>>> start auto ftun"
start_line=$(grep -n "$start_marker" ~/.bashrc | cut -d ":" -f 1)

# 搜索文件中的 # <<<< end auto ftun
end_marker="# <<<< end auto ftun"
end_line=$(grep -n "$end_marker" ~/.bashrc | cut -d ":" -f 1)

# 如果找到了这两个标记，则删除它们之间的所有行
if [[ -n "$start_line" && -n "$end_line" ]]; then
  sed -i "${start_line},${end_line}d" ~/.bashrc
fi





##############################################
# 插入内容
cat <<EOF > /tmp/this.txt

# >>>> start auto ftun

export PATH=/opt/ftun:\$PATH
export FTUN_SERVER=http://mqtt.nicelong.top:5001
export FTUN_API_KEY_FILE=/etc/apikeys.txt

# <<<< end auto ftun

EOF

# TODO 去掉原始内容
cat /tmp/this.txt >> ~/.bashrc

rm /tmp/this.txt


#############################################

ID=$(cat /etc/random-id)

if [ "${ID}" == "" ];
then
    hexdump  /dev/random  -n 100 > /tmp/randomid.txt 
    md5sum  /tmp/randomid.txt   | awk '{print $1}'  > /etc/random-id
fi
rm /tmp/randomid.txt

ID=$(cat /etc/random-id)
sed  /etc/ftunc.ini.default -e "s/{{ID}}/${ID}/g" > /etc/ftunc.ini